﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
using Eb.MessageBox;

public partial class Modules_UserManager_ucUserGroupMap : System.Web.UI.UserControl
{
    UserManager _userGroupMapManager = new UserManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetUsers();
        }
    }
    void GetUsers()
    {
        //users coming under the selected group
        string condition = "UR_UserGroupID = '" + Session["UserManager_UserGroupID"].ToString() + "' and  UR_UserID = UD_UserPersonalID";
        DataSet ds = _userGroupMapManager.GetData("UserDetails", "UserGroupMap", condition);
        lstSelectedUsers.DataSource = ds;
        lstSelectedUsers.DataTextField = "UD_FirstName";
        lstSelectedUsers.DataValueField = "UD_UserPersonalID";
        lstSelectedUsers.DataBind();

        //users not under the selected group
        condition = "UR_UserGroupID <> '" + Session["UserManager_UserGroupID"].ToString() + "' OR UT_UtilityGroupID IS NULL";
        condition = "UD_UserPersonalID not in (select UR_UserID from UserGroupMap where UR_UserGroupID = '" + Session["UserManager_UserGroupID"] + "')";
        ds.Clear();
        ds = _userGroupMapManager.GetData("UserDetails", condition);
        lstUsers.DataSource = ds;
        lstUsers.DataTextField = "UD_FirstName";
        lstUsers.DataValueField = "UD_UserPersonalID";
        lstUsers.DataBind();

    }

    
    protected void btnMove1_Click(object sender, EventArgs e)
    {
        if (lstUsers.SelectedIndex == -1)
        {
            MessageBox.Show("Select user");
          //  Response.Write("<script>alert('Select User')</script>");
            return;
        }

        lstSelectedUsers.Items.Add(new ListItem(lstUsers.SelectedItem.ToString(), lstUsers.SelectedValue));
        lstUsers.Items.RemoveAt(lstUsers.SelectedIndex);

    }
    protected void btnMove2_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstUsers.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstSelectedUsers.Items.Count; j++)
                if (lstUsers.Items[i].Value == lstSelectedUsers.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstSelectedUsers.Items.Add(new ListItem(lstUsers.Items[i].Text, lstUsers.Items[i].Value));
            }
        }
        lstUsers.Items.Clear();
    }

    protected void btnMove3_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstSelectedUsers.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstUsers.Items.Count; j++)
                if (lstSelectedUsers.Items[i].Value == lstUsers.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstUsers.Items.Add(new ListItem(lstSelectedUsers.Items[i].Text, lstSelectedUsers.Items[i].Value));
            }
        }
        lstSelectedUsers.Items.Clear();
    }

    protected void btnMove4_Click(object sender, EventArgs e)
    {
        if (lstSelectedUsers.SelectedIndex == -1)
        {
            MessageBox.Show("Select User");
          //  Response.Write("<script>alert('Select User')</script>");
            return;
        }
        lstUsers.Items.Add(new ListItem(lstSelectedUsers.SelectedItem.ToString(), lstSelectedUsers.SelectedValue));
        lstSelectedUsers.Items.RemoveAt(lstSelectedUsers.SelectedIndex);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucSearchUserGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {      
            AddUsers();
            RemoveUsers(); 
    }
       

    void AddUsers() //add users to the group
    {

        bool result = true;
        if (lstSelectedUsers.Items.Count == 0)
        {
            MessageBox.Show("There Is No User In The Group");
          //  Response.Write("<script>alert('There Is No User In The Group')</script>");
            return;
        }
        for (int i = 0; i < lstSelectedUsers.Items.Count; i++)
        {
            result = _userGroupMapManager.UserGroupMap(Session["UserManager_UserGroupID"].ToString(), lstSelectedUsers.Items[i].Value);

        }
        MessageBox.Show("Successfully Updated");
       // Response.Write("<script>alert('Updated')</script>");
    }

    void RemoveUsers() //remove utilities
    {
        string condition;

        if (lstUsers.Items.Count != 0)
        {

            condition = "UR_UserGroupID= '" + Session["UserManager_UserGroupID"].ToString() + "' and (UR_UserID='" + lstUsers.Items[0].Value + "'";
            for (int i = 1; i < lstUsers.Items.Count; i++)
            {
                condition = condition + " or UR_UserID = " + lstUsers.Items[i].Value + "";
            }
            condition = condition + ")";
            _userGroupMapManager.RemoveUsersFromGroup(condition);
        }
    }

}
